#!/usr/bin/env python
#
# Copyright (C) 2009 Anders Logg
#
# This file is part of DOLFIN.
#
# DOLFIN is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# DOLFIN is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
#
# Check and fix coding style in all files (currently very limited)

import os, sys, re

# Camelcaps exceptions
camelcaps_exceptions = ["cGq", "dGq",                                                         # ODE solver
                        "addListener", "addTest", "getRegistry", "makeTest", "wasSuccessful", # cppunit
                        "tagName", "tagNames", "documentElement", "childNodes",               # doscstrings
                        "nodeType", "firstChild", "parentNode",                               # docstrings
                        "doubleArray", "intArray",                                            # swig 
                        "fatalError", "xmlChar", "xmlNode", "xmlStrcasecmp",                  # libxml2
                        "startDocument", "endDocument", "startElement", "endElement"]         # libxml2

def camelcaps2underscore(word):
    "Convert fooBar to foo_bar."
    new_word = ""
    for c in word:
        if c.isupper() and len(new_word) > 0:
            new_word += "_" + c.lower()
        else:
            new_word += c
    return new_word

def check_camelcaps(code, replacements):
    "Replace fooBar by foo_bar."
    words = re.findall(r"\b([a-z]+[A-Z][a-z]+)\b", code)
    for word in words:
        if word in camelcaps_exceptions:
            continue
        new_word = camelcaps2underscore(word)
        code = code.replace(word, new_word)
        r = (word, new_word)
        if r in replacements:
            replacements[r] += 1
        else:
            replacements[r] = 1
        print "  Replacing: %s --> %s" % r
    return code

def check_trailing_whitespace(code, replacements):
    "Remove trailing spaces at end of lines."
    lines = []
    for line in code.split("\n"):
        new_line = line.rstrip()
        lines.append(new_line)
        if new_line == line:
            continue
        r = ("trailing whitespace", "")
        if r in replacements:
            replacements[r] += 1
        else:
            replacements[r] = 1
        print "  Removing trailing whitespace"
    new_code = "\n".join(lines)
    if len(new_code) > 0 and not new_code[-1] == "\n":
        new_code += "\n"
    return new_code

def replace(filename, replacements):
    "Make replacements for given filename."

    print "Checking %s..." % filename

    # Open file
    f = open(filename, "r")
    code = f.read()
    f.close()

    # Checks
    new_code = check_camelcaps(code, replacements)
    new_code = check_trailing_whitespace(code, replacements)

    # Write file
    f = open(filename, "w")
    f.write(new_code)
    f.close()

# Iterate over all source files
replacements = {}
for root, dirs, files in os.walk("dolfin"):
    for file in files:
        if not file.endswith((".cpp", ".h", ".py", ".i")):
            continue
        filename = os.path.join(root, file)
        replace(filename, replacements)
for root, dirs, files in os.walk("demo"):
    for file in files:
        if not file.endswith((".cpp", ".h", ".py")):
            continue
        filename = os.path.join(root, file)
        replace(filename, replacements)

# Report replacements
if len(replacements) == 0:
    print "No replacements were made, unchanged."
else:
    print ""
    print "The following replacements were made:"
    lines = []
    for r in replacements:
        lines.append("  %s --> %s (%d occurences)" % (r[0], r[1], replacements[r]))
    lines.sort()
    print "\n".join(lines)
